import { createStore, applyMiddleware, combineReducers } from "../myredux";
import isPromise from "is-promise";

// 定义修改规则
function countReducer(state = 0, action) {
  switch (action.type) {
    case "ADD":
      return state + 1;
    case "MINUS":
      return state - action.payload || 1;
    default:
      return state;
  }
}

const store = createStore(
  // countReducer,
  combineReducers({ count: countReducer }),
  applyMiddleware(thunk, promise, logger)
);

export default store;

// 处理异步的中间件
function thunk({ getState, dispatch }) {
  return function thunkHandler(next) {
    const strongDispatch = (action) => {
      if (typeof action === "function") {
        return action(dispatch, getState);
      }
      return next(action);
    }
    // 返回一个加强的dispatch给下一个中间件
    return strongDispatch;
  };
}

// 打印日志
function logger({ getState, dispatch }) {
  return function loggerHandler(next) {

    return (action) => {
      console.log("------------------------------"); //sy-log

      console.log(action.type + "执行了！"); //sy-log

      const preState = getState();

      console.log("prev state", preState); //sy-log

      const returnValue = next(action);
      const nextState = getState();

      console.log("next state", nextState); //sy-log

      console.log("------------------------------"); //sy-log
      return returnValue;
    }
  };
}


function promise({ dispatch }) {
  return function promiseHandler(next) {
    console.log(next)
    return (action) => {
      return isPromise(action) ? action.then(dispatch) : next(action);
    };
  }
}
